#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
Search the lucene index and retrieve a list of results.
"""

import utility

def searchloop(results, pagestartnum, pageendnum):
	# return list
	resultlist = []

	# current count in the loop
	current = 1

	# loop through the results
	try:	# catch pylucene psp error
		for i, doc in results:
			# if loop count is between page start and end
			if current >= pagestartnum and current <= pageendnum:
				# load record fields into a dict
				record = {}
				try:	# catch pylucene psp error
					for field in doc:
						record[field.name()] = field.stringValue()
				except: pass

				# escape brackets (prevent html passing through)
				record = dict([(key, utility.gtltescape(record[key])) for key in record])

				# add to list
				resultlist.append(record)
				

			# if we are past the record for this page
			if current >= pageendnum:
				# stop looping
				break

			# increment count
			current += 1
	except: pass
	
	# return the results
	return resultlist

# eof

